package com.wxmit.module.system.web;

import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 *  通用操作的controller
 * @Author huangxiong
 * @Date 2022/8/4 15:08
 * @Version V1.0
 * @tag common
 */
@RestController
@RequestMapping("common")
@RequiredArgsConstructor
public class CommonController {

    private final FileStorageService fileStorageService;

    /**
     * 通用上传请求 （模块名）
     * @param file 文件
     * @param module 模块名
     * @return 文件url地址
     * @throws IOException
     */
    @PostMapping("/upload/{module}")
    public String uploadFile(MultipartFile file, @PathVariable("module") String module) throws IOException {
        FileInfo fileInfo = fileStorageService.of(file).setPath(module + File.separator).upload();
        return fileInfo.getUrl();
    }

    /**
     * 通用上传请求
     * @param file 文件
     * @return 文件url地址
     * @throws IOException
     */
    @PostMapping("/upload/")
    public String uploadFile(MultipartFile file){
        FileInfo fileInfo = fileStorageService.of(file).setPath("common" + File.separator).upload();
        return fileInfo.getUrl();
    }


}
